//
//  TTWelcomeModel.m
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/24.
//  Copyright © 2016年 陶路路. All rights reserved.
//

#import "TTWelcomeModel.h"
#import "TTNetworkingTools.h"
#import "TTWelconmeModel.h"

@interface TTWelcomeModel ()

@end
@implementation TTWelcomeModel


// 单例
+(instancetype)sharedManager{
    
    static TTWelcomeModel * instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[super alloc]init];
        
    });
    
    return instance;
}

-(instancetype)init{
    
    if (self = [super init]) {
        
        self.model = [self getUserImfo];
    }
    
    return self;
}

#warning 两个时间比较

-(BOOL)islogin{

    if (self.model.access_token != nil) {
        
        if ([self.model.created_at compare:[NSDate date]] ==  NSOrderedDescending) {
            
            return YES;
        }else{
            
            return NO;
        }
    }else{
        
        return NO;
    }
}

// 归档 与 解档

- (void)saveUserInfo:(TTWelconmeModel*)model{
    
    [NSKeyedArchiver archiveRootObject:model toFile:FILEPATH];
}

// 解档

- (TTWelconmeModel*)getUserImfo{
    
   TTWelconmeModel* model =  [NSKeyedUnarchiver unarchiveObjectWithFile:FILEPATH];
    
    return model;
}


// 网络请求

-(void)getUserAccount:(void(^)(BOOL isSucc))success code:(NSString*)code{
   
    [[TTNetworkingTools sharedManager]loadWelcomeDatasucessBlock:code sucessBlock:^(NSDictionary* responseObject) {
        
        self.model = [[TTWelconmeModel alloc]initWithDictionary:responseObject];
        
        [self getUserInfo:self.model success:success];
        
    } error:^(NSError *error) {
        
        success(NO);
        
        NSLog(@"%@",error);
    }];
}

// 网咯请求

-(void)getUserInfo:(TTWelconmeModel*)model success:(void(^)(BOOL isSucc))success{
    
    [[TTNetworkingTools sharedManager] loadUserInfo:model sucessBlock:^(NSDictionary* responseObject) {
        
        model.screen_name = responseObject[@"screen_name"];
        
        model.avatar_large = responseObject[@"avatar_large"];
        
        [self saveUserInfo:model];
        
        success(YES);
        
    } error:^(NSError *error) {
        
        success(NO);
    }];
}
@end
